package tp.pr3;

import java.util.Scanner;

import tp.pr3.commands.Command;
import tp.pr3.commands.exceptions.CommandExecutionException;
import tp.pr3.commands.exceptions.WrongCommandFormatException;

public class Game
{

	private Map _mapa;
	private Room _habitacionActual;

	private Player _friki;
	private boolean _exit;

	public void setCurrentRoom(Room currentRoom)
	{
		this._habitacionActual = currentRoom;
	}

	public Game(Map map)
	{
		try
		{
			this._mapa = map;
			this._habitacionActual = map.getCurrentRoom();
			this._friki = new Player();
			this._exit = false;
		} catch (Exception e)
		{
			this._exit=true;
		}

	}

	public void setSalida(boolean exit)
	{
		this._exit = exit;
	}


	public Room getCurrentRoom()
	{
		return this._habitacionActual;
	}

	public Player getPlayer()
	{
		return this._friki;
	}

	public Map getCurrentMap()
	{
		return this._mapa;
	}

	public void runGame()
	{
		try
		{
			if (this._exit) throw new Exception("El mapa es null. A la porra el juego");
			Scanner sc = new Scanner(System.in);
			showStatus();
			System.out.print("> ");
			while (!_exit && !this._friki.dead())
			{
				String line = sc.nextLine();
				try
				{
					Command auxCommand = Parser.parseCommand(line, this);
					auxCommand.execute();
				} catch (WrongCommandFormatException e)
				{
					System.out.print(e.getMessage());
				} catch (CommandExecutionException e1)
				{
					System.out.print(e1.getMessage());
				}

			}
		} catch (Exception e)
		{
			System.out.println(e.getMessage());
		}

	}
	private void showStatus()
	{

		System.out.println(this._habitacionActual.getDescription()
				+ "\nHEALTH = " + String.valueOf((this._friki.getHealth()))
				+ ", SCORE =" + String.valueOf((this._friki.getPoints())));
	}
	
	public void requestQuit()
	{
		if (!this._exit)
		{
			this._exit = true;
		}
	}

}